﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using DevExpress.Web.Mvc;
using Webcar.BLL;
using Webcar.Entitys;
using Webcar.Utils;
using System.Data.Entity;
using System.Web.UI.WebControls;
using System.Threading;
using System.Data;
using EntityFramework.Extensions;
namespace Webcar.Controllers
{
    
    public partial class WorkController : BaseControler
    {
        public ActionResult List()
        {
            return View();
        }

        public ActionResult ListPartialView()
        {
            return PartialView();
        }

        public ActionResult SloganList()
        {
            return View();
        }

        public ActionResult SloganListPartialView()
        {
            return PartialView();
        }
        public ActionResult SloganDelete(int id)
        {
            CurrentUser.EnsureAdmin();//(PermissionManager.发布工作动态);
            using (var DB = new WebcarContext())
            {
                var model = DB.slogan_info.Find(id);
                if (model != null)
                {
                    DB.slogan_info.Remove(model);
                    DB.SaveChanges();
                }
            }
            return Success("SloganList", "Work");
        }
        public ActionResult Delete(int id)
        {
            CurrentUser.EnsureAdmin();//(PermissionManager.发布工作动态);
            using (var DB = new WebcarContext())
            {
                var model = DB.work_dynamic.Find(id);
                if (model != null)
                {
                    DB.work_dynamic.Remove(model);
                    DB.SaveChanges();
                }
            }
            return Success("List", "Work");
        }
        [HttpGet]
        public ActionResult View(int? id)
        {
            var model = WebcarDataProvider.DB.work_dynamic.Find(id);
            return View(model: model);
        }
        [HttpGet]
        public ActionResult SloganEdit(int? id)
        {
            slogan_info model = null;
            if (id != null)
            {
                model = WebcarDataProvider.DB.slogan_info.Find(id);
            }
            if (model == null)
            {
                model = new slogan_info();
                model.is_use = true;
            }
            return View(model: model);
        }
        [ValidateInput(false)]
        [HttpPost]
        public ActionResult SloganEdit(slogan_info model)
        {
            CurrentUser.EnsureAdmin();// (PermissionManager.发布工作动态);
            if (model.note.IsNull() || model.note.IsNull())
            {
                ViewBag.ErrorMessage = "请输入内容。";
                return View(model);
            }
            model.data_time = DateTime.Now;
            using (var DB = new WebcarContext())
            {
                slogan_info item = DB.slogan_info.Find(model.id);
                if (item == null)
                {
                    DB.slogan_info.Add(model);
                }
                else
                {
                    model.CopyPropertyTo(item);
                    item.data_time = DateTime.Now;
                }

                DB.SaveChanges();
            }
            return Success("SloganList", "Work");
        }

        [HttpGet]
        public ActionResult Edit(int? id)
        {
            work_dynamic model = null;
            if (id!=null)
            {
                model = WebcarDataProvider.DB.work_dynamic.Find(id);
            }
            if (model == null)
            {
                model = new work_dynamic();
                model.is_use = true;
            }
            return View(model: model);
        }
        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Edit(work_dynamic model)
        {
            CurrentUser.EnsureAdmin();// (PermissionManager.发布工作动态);
            if (model.title.IsNull()||model.html_content.IsNull())
            {
                ViewBag.ErrorMessage = "请输入标题和内容。";
                return View(model);
            }
            string filename = "";
            string filename2 = "";
            string filename3 = "";

            if (Request.Files.IsEmpty()==false)
            {
                filename = Request.Files.UploadImage("work", 480, 320, "W");
            }
            if (Request.Files.IsEmpty(1)==false)
            {
                filename2 = Request.Files.UploadImage("work", 480, 320, "W",1);
            }
            if (Request.Files.IsEmpty(2)==false)
            {
                filename3 = Request.Files.UploadImage("work", 480, 320, "W",2);
            }

            work_dynamic item = null;
            using (var DB = new WebcarContext())
            {
                if (model.id > 0)
                {
                    item = DB.work_dynamic.Find(model.id);
                    item.title = model.title;
                    item.title2 = model.title2;
                    item.html_content = model.html_content;
                    if (filename.IsNotNull())
                        item.image_url = filename;
                    if (filename2.IsNotNull())
                        item.image_url2 = filename2;
                    if (filename3.IsNotNull())
                        item.image_url3 = filename3;

                }
                else
                {
                    model.data_time = DateTime.Now;
                    model.OPERATOR = CurrentUser.GetLoginUserName();
                    if (filename.IsNotNull())
                        model.image_url = filename;
                    if (filename2.IsNotNull())
                        model.image_url2 = filename2;
                    if (filename3.IsNotNull())
                        model.image_url3 = filename3;
                    DB.work_dynamic.Add(model);
                }

                DB.SaveChanges();
            }
            return Success("List", "Work");
        }
    }
}